/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.ViewState;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.LastZoomCommand;

/**
 * A class representing creation dialog on screen.<br>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.Interface
 * @since July 2004
 */
public final class GeneralSettingsDialog extends SimpleDialog {

	private static final long serialVersionUID = 1L;
	private LearnInterface parent;
    private JPanel generalPane = new JPanel();
    private JPanel colorsPane = new JPanel();
    private JPanel genPane = new JPanel();
    
		
    private String[] precisionStr = {"0", "0.0", "0.00", "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000"};
    private JComboBox precision = new JComboBox(precisionStr);
    private NumberFormat numberFormat;
    private ViewState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.Interface
     */
    public GeneralSettingsDialog(LearnInterface parent, ViewState s) {
        this.parent = parent;
        state = s;
		
        setUpFormat();
		
        //Sets fields according to model
        precision.setSelectedIndex(state.getPrecision());
        
		//Sets the colors panel
        colorsPane.setLayout(new GridLayout(10,3,2,2));
        colorsPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Colors"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		colorsPane.setMaximumSize(new Dimension(500,300));
        
				
        //Sets internal general panel
        genPane.setLayout(new GridLayout(1,2,2,2));
        genPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Precision"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		genPane.setMaximumSize(new Dimension(500,55));
        genPane.add(new JLabel(" Precision:"));
        genPane.add(precision);
		
        //Sets general panel
        generalPane.setLayout(new BoxLayout(generalPane, BoxLayout.Y_AXIS));
        generalPane.add(genPane);
        generalPane.add(colorsPane);
		
		setPane(generalPane);
		
    }
	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(parent, "General Settings");
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            
			
			state.setPrecision(precision.getSelectedIndex());
			
			parent.setModel(parent.getModel());
            this.setVisible(false);
			
			LastZoomCommand cmd = new LastZoomCommand(parent);
			cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    

	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}
